package org.nuxeo.gwt.habyt.demo.client;

import org.nuxeo.gwt.habyt.app.client.js.JsBridge;
import org.nuxeo.gwt.habyt.app.client.js.JsEvent;
import org.nuxeo.gwt.habyt.app.client.js.JsEventHandler;
import org.nuxeo.gwt.habyt.invocation.client.ObjectRef;
import org.nuxeo.gwt.habyt.templates.client.RootTemplate;
import org.nuxeo.gwt.habyt.templates.client.TemplatePanel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint {

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
            @Override
            public void onValueChange(ValueChangeEvent<String> event) {
                System.out.println("######### " + event.getValue());
            }
        });
        JsBridge.install();
        JsBridge.addHandler(new JsEventHandler() {
            public void onJsEvent(JsEvent event) {
                Window.alert("JsEvent:" + event.getName() + "; "
                        + event.getTarget() + "; " + event.getData());
            }
        });

        final AsyncCallback<TemplatePanel> cb = new AsyncCallback<TemplatePanel>() {
            public void onSuccess(TemplatePanel result) {
                // System.out.println(RootTemplate.get().findWidget("section"));
            }

            public void onFailure(Throwable caught) {
                GWT.getUncaughtExceptionHandler().onUncaughtException(caught);
            }
        };

        // RootPanel.get().add(RootTemplate.wrap("app", cb));
        // RootPanel.get().add(RootTemplate.loadTemplate("app.html", cb));
        RootPanel.get().add(
                RootTemplate.loadTemplate(new ObjectRef("file:app.html"), cb));
        // RootPanel.get().add(new Button("Test", new ClickHandler() {
        //
        // @Override
        // public void onClick(ClickEvent event) {
        // // Application.get().getBusy(
        // // (Widget) Application.get().getView("editor")).show();
        // Application.get().showMessage("bbbbbbbb");
        // }
        // }));

        // System.out.println(RootTemplate.get().findWidget("section"));
    }
}
